// 订单列表API服务
import request from "@/common/utils/request.js";

const BASE_URL = "/api/order/tableList";

// 获取订单列表
export const getOrderList = (params) => {
  // uni.request 对于 GET 请求需要使用 data 作为查询参数
  return request({
    url: BASE_URL,
    method: "GET",
    data: params,
  });
};

// 获取订单详情
export const getOrderDetail = (orderId) => {
  return request({
    url: `/api/order/${orderId}`,
    method: "GET",
  });
};

// 取消订单
export const cancelOrder = (orderId, reason) => {
  return request({
    url: `/api/order/${orderId}/cancel`,
    method: "POST",
    data: { reason },
  });
};

// 确认收货
export const confirmReceive = (orderId) => {
  return request({
    url: `/api/order/${orderId}/complete`,
    method: "POST",
  });
};

// 申请退货
export const applyRefund = (orderId, reason, returnItems = null) => {
  const data = { reason };
  if (returnItems && returnItems.length > 0) {
    data.returnItems = returnItems;
  }
  return request({
    url: `/api/order/${orderId}/return/request`,
    method: "POST",
    data,
  });
};

// 提醒发货
export const remindDeliver = (orderId) => {
  return request({
    url: `/api/order/${orderId}/remind`,
    method: "POST",
  });
};

// 查看物流
export const getLogistics = (orderId) => {
  return request({
    url: `/api/order/${orderId}/logistics`,
    method: "GET",
  });
};

// // 评价订单
// export const reviewOrder = (orderId, reviewData) => {
//   return request({
//     url: `/api/order/${orderId}/review`,
//     method: "POST",
//     data: reviewData
//   });
// };

// 再次购买
export const buyAgain = (orderId) => {
  return request({
    url: `/api/order/${orderId}/buy-again`,
    method: "POST",
  });
};

// 创建订单
export const creatOrder = (data) => {
  return request({
    url: `/api/order/add`,
    method: "POST",
    data,
  });
};

// 发票相关API
// 保存发票抬头信息
export const saveInvoiceInfo = (orderId, invoiceData) => {
  return request({
    url: `/api/order/${orderId}/invoice/info`,
    method: "PATCH",
    data: invoiceData,
  });
};

// 获取发票信息
export const getInvoiceInfo = (orderId) => {
  return request({
    url: `/api/order/${orderId}/invoice`,
    method: "GET",
  });
};

// 导出订单
export const exportOrderList = (params) => {
  return request({
    url: "/api/export/orders",
    method: "POST",
    data: params,
  });
};

// 下载导出文件
export const downloadExportFile = (filename) => {
  return request({
    url: `/api/file/download/${filename}`,
    method: "GET",
    responseType: "blob", // 用于下载文件
  });
};

// 预览导出文件信息
export const previewExportFile = (filename) => {
  return request({
    url: `/api/file/preview/${filename}`,
    method: "GET",
  });
};

// 获取导出进度
export const getExportProgress = (taskId) => {
  return request({
    url: `/api/export/progress/${taskId}`,
    method: "GET",
  });
};
